package org.example.core;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

/**
 * 谷歌翻译类
 */
public class GT {

    private static final String PATH1="https://translate.google.cn/?sl=auto&tl=en&";

    private static final String USER_AGENT="Mozilla/5.0";//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";


    private static GT _instance = null;

    /**
     * 获取单例
     * @return
     */
    public static GT getInstance() {
        if( null == _instance){
            _instance = new GT();
        }
        return _instance;
    }


    /**
     * 判断语言是否支持
     * @param language
     * @return
     */
    public boolean isSupport(String language){
        if( null == Controller.LANGUAGE_MAP.get( language )){
            return false;
        }
        return true;
    }


    /**
     * 翻译文本
     * @param text  文本内容
     * @param sourceLang  文本所属语言。如果不知道，可以使用auto
     * @param targetLang  目标语言。必须是明确的有效的目标语言
     * @return
     * @throws Exception
     */
    public void translateText(String text,String sourceLang, String targetLang) throws Exception{
        if( !( isSupport(sourceLang) || isSupport(targetLang) ) ){
            throw new Exception("不支持的语言类型");
        }
        postHttp(PATH1,text);
    }

    /**
     * 发送请求道google
     *
     * @param
     * @return
     */
    private void postHttp(String path,String text) {
        WebDriver driver = new ChromeDriver();
//        driver.manage().window();
        StringBuilder sb1 = new StringBuilder(path);
        sb1.append("text=");
        sb1.append(text);
        sb1.append("&op=translate");
        driver.get(sb1.toString());
        Actions action = new Actions(driver);
        Controller controller = new Controller();
        Controller.LANGUAGE_MAP.entrySet().forEach(e -> {
            String key = e.getKey();
            try {
                action.click(driver.findElement(By.xpath("//*[@id=\"yDmH0d\"]/c-wiz/div/div[2]/c-wiz/div[2]/c-wiz/div[1]/div[1]/c-wiz/div[5]/button"))).perform();
                Thread.sleep(500);
                WebElement element = driver.findElement(By.xpath(
                        "//*[@id=\"yDmH0d\"]/c-wiz/div/div[2]/c-wiz/div[2]/c-wiz/div[3]/c-wiz/div[2]/div/div[3]/div/div[2]/div[@data-language-code=\""+key+"\"]"
                ));
                action.click(element).perform();
                Thread.sleep(600);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
                controller.flag = false;
                driver.quit();
            }
            WebElement result_text =
                    driver.findElement(By.xpath("//*[@id=\"yDmH0d\"]/c-wiz/div/div[2]/c-wiz/div[2]/c-wiz/div[1]/div[2]/div[2]/c-wiz[2]/div[5]/div/div[1]"));
            controller.save2File(result_text.getText(),key);
            System.out.println("current key is succeed = "+key);
        });
        controller.flag = false;
        driver.quit();
        System.out.println("-----end-----");
    }


}